/**
 *
 * @author John Creecy
 */
package battlebarnserver;

import battlebarnserver.entities.PlayerEntity;
import battlebarnserver.entities.TileEntity;
import battlebarnserver.util.Conversions;

public class ProcessorThread implements Runnable {

	private String packetData;
	private String sourceIp;
	private int movementDirection; // 0 none, 1 up, 2 right, 3 down, 4 left
	private int movementUnits;
	private int tileX;
	private int tileY;

	public ProcessorThread(String packetDataString, String ipAddress, int port) {
		this.packetData = packetDataString;
		this.sourceIp = ipAddress;
		movementUnits = 1;
	}

	public void run() {
		processPacketData();
	}

	private void processPacketData() {
		if(BattleBarn.actionStack.containsKey(sourceIp)) {
			PlayerEntity previousPlayerEntity = BattleBarn.actionStack.get(sourceIp);
			tileX = previousPlayerEntity.getTileX();
			tileY = previousPlayerEntity.getTileY();
		} else {
			tileX = 0;
			tileY = 0;
		}

		movementDirection = Integer.parseInt(this.packetData.substring(0,1));
	
		// 0 none, 1 up, 2 right, 3 down, 4 left
		switch(movementDirection) {
			case 1: tileY=(tileY-movementUnits); break;
			case 2: tileX=(tileX+movementUnits); break;
			case 3: tileY=(tileY+movementUnits); break;
			case 4: tileX=(tileX-movementUnits); break;
			default: break;
		}

		PlayerEntity playerEntity = new PlayerEntity(sourceIp, 100, Conversions.getBoundsX(tileX), Conversions.getBoundsY(tileY));
		BattleBarn.actionStack.put(sourceIp, playerEntity);
	}
}
